
import 'dart:async';

import 'package:cunyutech_flutter_ui/cunyutech_flutter_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class LogConsole extends StatefulWidget {
  LogConsole({Key key}) : super(key: key);

  @override
  _LogConsoleState createState() => _LogConsoleState();
}

class _LogConsoleState extends State<LogConsole> {

  List<Widget> children = [];
  StreamSubscription<CYLoggerData> subscription;

  @override
  void initState() {
    subscription = CYLogger.prints.stream.listen(this.handleAddData);
    super.initState();  
  }

  @override
  void dispose() { 
    if(subscription != null) subscription.cancel();
    super.dispose();
  }

  Color textColor(String level) {
    Color defaultColor = Colors.white;
    switch (level) {
      case "error":
        defaultColor = Colors.red;
        break;
      case "warning":
        defaultColor = Colors.orange;
        break;
        break;
      default:
        break;
    }
    return defaultColor;
  }

  handleAddData(CYLoggerData data) {
    children.insert(0, RichText(
      maxLines: 5,
      overflow: TextOverflow.ellipsis,
      textAlign: TextAlign.left,
      text: TextSpan(
        style: TextStyle(fontSize: 8, color: textColor(data.level)),
        children: [
          TextSpan(
            text: "[${data.level}]",
          ),
          TextSpan(
            text: "[${CYDateFormat.toZone8StringFromUtc(data.createAt)}]",
          ),
          TextSpan(
            text: "[${data.tag}]",
          ),
          TextSpan(
            text: "${data.message}",
          ),
        ]
      ),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       width: 320,
       height: 200,
       child: Column(
         mainAxisAlignment: MainAxisAlignment.start,
         children: <Widget>[
            Container(
              height: 20,
              color: Colors.white70,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Container( width: 44 ),
                  Text("日志打印", style: TextStyle(fontSize: 10, color: Colors.black45),),
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        children.clear();
                      });
                    },
                    child: Container(
                      width: 44,
                      alignment: Alignment.center,
                      child: Text("清除", style: TextStyle(fontSize: 10, color: Colors.black45),),
                    ) 
                  )
                ],
              ) 
            ),
           Container(
            height: 180,
            width: 320,
            decoration: BoxDecoration(
              color: Colors.black
            ),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: children,
              )
            )
          )
         ],
       )
    );
  }
}